package controller.hansot;

import hansot.to.CategoryTO;
import hansot.to.CommentTO;
import hansot.to.GradeTO;
import hansot.to.MemberTO;
import hansot.to.MenuTO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.hansot.MenuService;

@Controller
public class MenuController {
	private MenuService service;

	public MenuController(MenuService service) {
		this.service = service;
	}
	@RequestMapping("/menuView.do")
	public ModelAndView menuView(HttpServletRequest request,HttpServletResponse response,HttpSession session,String menuId) {
		ModelAndView mv = new ModelAndView();
		
		try {
			MenuTO muto = service.getMenuById(menuId);
			mv.addObject("muto",muto);
			ArrayList<CommentTO> commentList = service.getCommentByMenuId(menuId);
			mv.addObject("commentList",commentList);
			mv.setViewName("/view_menu");
		} catch (SQLException e) {
			mv = new ModelAndView("/error", "error_message",
					"이름으로 검색도중 오류발생 : " + e.getMessage());
		}
		
		return mv;
	}
	@RequestMapping("/foodMenu.do")
	public ModelAndView selectMenuByCategory(String categoryId){
		ModelAndView mv = null;
		try{
			ArrayList<MenuTO> list = service.selectMenuByCategory(categoryId);
			int count = list.size();
			HashMap map = new HashMap();
			map.put("list", list);
			map.put("count", count);
			mv = new ModelAndView("foodMenu/category","category",map);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("/orderPage.do")
	public ModelAndView orderPage(){
		ModelAndView mv = null;
		try{
			ArrayList<CategoryTO> list = service.selectCategory();
			int count = list.size();
			HashMap map = new HashMap();
			map.put("category", list);
			map.put("num", count);
			mv = new ModelAndView("foodMenu/orderPage","info",map);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("/addGrade.do")
	public ModelAndView giveGrade(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView mv = new ModelAndView();
		try {
			
			if(session.getAttribute("login_info") == null){
				mv.addObject("error_message","로그인 후 사용하세요");
				mv.setViewName("/view_menu");
				return mv;
			}else{
				MemberTO mto = (MemberTO) session.getAttribute("login_info");
				GradeTO gto = new GradeTO();
				gto.setMemberId(mto.getMemberId());
				gto.setMenuId(request.getParameter("menuId"));
				gto.setGradeNumber(Integer.parseInt(request.getParameter("grSel")));
				service.addMenuGrade(gto);
				
				MenuTO muto = service.getMenuById(request.getParameter("menuId"));
				mv.addObject("muto",muto);
				ArrayList<CommentTO> commentList = service.getCommentByMenuId(request.getParameter("menuId"));
				mv.addObject("commentList",commentList);
				
			}
			mv.setViewName("/view_menu");
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		
		return mv;
	}
	
	@RequestMapping("/commentWrite.do")
	public ModelAndView commentWrite(HttpServletRequest request,HttpServletResponse response,HttpSession session,String com){
		ModelAndView mv = new ModelAndView();
		CommentTO co = new CommentTO();
		try {
			
			if(session.getAttribute("login_info") == null){
				mv.addObject("error_message","로그인 후 사용하세요");
				mv.setViewName("/view_menu");
				return mv;
			}else{
				MemberTO mto = (MemberTO) session.getAttribute("login_info");
				co.setCommentContent(request.getParameter("comm"));
				co.setWriter(mto.getMemberId());
				co.setMenuId(request.getParameter("menuId"));
				service.insertComment(co);
				
				MenuTO muto = service.getMenuById(request.getParameter("menuId"));
				mv.addObject("muto",muto);
				ArrayList<CommentTO> commentList = service.getCommentByMenuId(request.getParameter("menuId"));
				mv.addObject("commentList",commentList);
			}
			mv.setViewName("/view_menu");
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("getMenu.do")
	public ModelAndView getMenu(String categoryId){
		ModelAndView mv = null;
		try{
			ArrayList<MenuTO> list = service.selectMenuByCategory(categoryId);
//			JSONArray array = new JSONArray(list);
//			System.out.println(array);
			mv = new ModelAndView("jsonView","menu",list);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("getMenuInfo.do")
	public ModelAndView getMenuInfo(String menuId){
		ModelAndView mv = null;
		try{
			MenuTO mto = service.getMenuById(menuId);
//			JSONObject jobj = new JSONObject(mto);
//			System.out.println(jobj);
			mv = new ModelAndView("jsonView","menuInfo",mto);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	
}
